/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.kmcsoft.goldshop.generic.service.impl;

import com.kmcsoft.emaster.entity.Employee;
import com.kmcsoft.goldshop.generic.service.ShopGenericService;
import com.kmcsoft.goldshop.model.master.Booth;
import com.kmcsoft.goldshop.model.master.Currency;
import com.kmcsoft.goldshop.model.master.DiamondColor;
import com.kmcsoft.goldshop.model.master.DiamondGroup;
import com.kmcsoft.goldshop.model.master.DiamondPure;
import com.kmcsoft.goldshop.model.master.GoldSource;
import com.kmcsoft.goldshop.model.master.Goldsmith;
import com.kmcsoft.goldshop.model.master.JewelryCost;
import com.kmcsoft.goldshop.model.master.JewelryStyle;
import com.kmcsoft.goldshop.model.master.PricingPerson;
import com.kmcsoft.goldshop.model.master.Product;
import com.kmcsoft.goldshop.model.master.ProductGold;
import com.kmcsoft.goldshop.model.master.Shop;
import com.kmcsoft.goldshop.model.master.Unit;
import com.kmcsoft.goldshop.model.scm.ImportJewelryDetail;
import com.kmcsoft.goldshop.model.scm.ImportJewerlyOrder;
import com.kmcsoft.master.defaultservice.DefaultGenericService;
import com.kmcsoft.utils.DateUtil;
import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.Date;

/**
 *
 * @author huyvu
 */
public class JdoShopGenericService extends DefaultGenericService implements ShopGenericService {

    //<editor-fold defaultstate="collapsed" desc="getDiamondGroups">
    public Collection<DiamondGroup> getDiamondGroups() {
        return getJdoTemplate().detachCopyAll(getJdoTemplate().find(DiamondGroup.class));
    }
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc="getGoldSJCs">
    public Collection<ProductGold> getGoldSJCs() {
        getPersistenceManager().getFetchPlan().addGroup("detach_ProductGold_Unit");
        getPersistenceManager().getFetchPlan().setMaxFetchDepth(-1);
        return getJdoTemplate().detachCopyAll(getJdoTemplate().find(ProductGold.class, "groupSJC==true"));
    }
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc="getNormalGolds">
    public Collection<ProductGold> getNormalGolds() {
        getPersistenceManager().getFetchPlan().addGroup("detach_ProductGold_Unit");
        getPersistenceManager().getFetchPlan().setMaxFetchDepth(-1);
        return getJdoTemplate().detachCopyAll(getJdoTemplate().find(ProductGold.class, "groupSJC==false"));
    }
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc="getBooths">
    public Collection<Booth> getBooths() {
        return getJdoTemplate().detachCopyAll(getJdoTemplate().find(Booth.class));
    }
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc="getShops">
    public Collection<Shop> getShops() {
        getPersistenceManager().getFetchPlan().addGroup("detach_Shop_Booth");
        getPersistenceManager().getFetchPlan().setMaxFetchDepth(-1);
        return getJdoTemplate().detachCopyAll(getJdoTemplate().find(Shop.class));
    }
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc="getGoldSources">
    public Collection<GoldSource> getGoldSources() {
        return getJdoTemplate().detachCopyAll(getJdoTemplate().find(GoldSource.class));
    }
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc="getJewelryStyles">
    public Collection<JewelryStyle> getJewelryStyles() {
        return getJdoTemplate().detachCopyAll(getJdoTemplate().find(JewelryStyle.class));
    }
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc="getJewelryCosts">
    public Collection<JewelryCost> getJewelryCosts() {
        return getJdoTemplate().detachCopyAll(getJdoTemplate().find(JewelryCost.class));
    }
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc="getGoldsmiths">
    public Collection<Goldsmith> getGoldsmiths() {
        return getJdoTemplate().detachCopyAll(getJdoTemplate().find(Goldsmith.class));
    }
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc="PricingPerson">
    public Collection<PricingPerson> getPricingPersons() {
        return getJdoTemplate().detachCopyAll(getJdoTemplate().find(PricingPerson.class));
    }
    //</editor-fold>
    
    //<editor-fold defaultstate="collapsed" desc="getCurrencyByCode">
    public Currency getCurrencyByCode(String code) {
        Collection<Currency> currencies = getJdoTemplate().find(Currency.class, "code.toLowerCase() ==pCode", "String pCode", code.toLowerCase());
        if (currencies.isEmpty()) {
            return null;
        }
        return getJdoTemplate().detachCopy(currencies.iterator().next());
    }
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc="getUnitByName">
    public Unit getUnitByCode(String code) {
        Collection<Unit> units = getJdoTemplate().find(Unit.class, "code.toLowerCase() ==pCode", "String pCode", code.toLowerCase());
        if (units.isEmpty()) {
            return null;
        }
        return getJdoTemplate().detachCopy(units.iterator().next());
    }
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc="checkUsingDiamondGroup">
    public boolean checkUsingDiamondGroup(DiamondGroup selectedRecord) {
        getPersistenceManager().getFetchPlan().addGroup("detach_DiamondGroup_ProductDiamond");
        getPersistenceManager().getFetchPlan().addGroup("detach_DiamondGroup_DiamondExchange");
        getPersistenceManager().getFetchPlan().setMaxFetchDepth(-1);
        DiamondGroup diamondGroup = getJdoTemplate().detachCopy(getJdoTemplate().getObjectById(DiamondGroup.class, selectedRecord.getId()));
        if (diamondGroup.getDiamonds().isEmpty() && diamondGroup.getExchanges().isEmpty()) {
            return false;
        }
        return true;
    }
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc="getUnits">
    public Collection<Unit> getUnits() {
        return getJdoTemplate().detachCopyAll(getJdoTemplate().find(Unit.class));
    }
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc="getDiamondColors">
    public Collection<DiamondColor> getDiamondColors() {
        return getJdoTemplate().detachCopyAll(getJdoTemplate().find(DiamondColor.class));
    }
    //</editor-fold>
    
    //<editor-fold defaultstate="collapsed" desc="getDiamondPures">
    public Collection<DiamondPure> getDiamondPures() {
        return getJdoTemplate().detachCopyAll(getJdoTemplate().find(DiamondPure.class));
    }
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc="checkUsingDiamondColor">
    public boolean checkUsingDiamondColor(DiamondColor selectedRecord) {
        getPersistenceManager().getFetchPlan().addGroup("detach_DiamondColor_All");
        getPersistenceManager().getFetchPlan().setMaxFetchDepth(-1);
        DiamondColor diamondColor = getJdoTemplate().detachCopy(getJdoTemplate().getObjectById(DiamondColor.class, selectedRecord.getId()));
        if (diamondColor.getExchangeDetails().isEmpty() && diamondColor.getProductDiamonds().isEmpty()) {
            return false;
        }
        return true;
    }
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc="checkUsingGoldSource">
    public boolean checkUsingGoldSource(GoldSource selectedRecord) {
        getPersistenceManager().getFetchPlan().addGroup("detach_GoldSource_ImportGoldOrder");
        getPersistenceManager().getFetchPlan().setMaxFetchDepth(-1);
        GoldSource goldSource = getJdoTemplate().detachCopy(getJdoTemplate().getObjectById(GoldSource.class, selectedRecord.getId()));
        if (goldSource.getImportOrders().isEmpty()) {
            return false;
        }
        return true;
    }
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc="checkUsingJewelryCost">
    public boolean checkUsingJewelryCost(JewelryCost selectedRecord) {
        getPersistenceManager().getFetchPlan().addGroup("detach_JewelryCost_ProductJewelry");
        getPersistenceManager().getFetchPlan().setMaxFetchDepth(-1);
        JewelryCost jewelryCost = getJdoTemplate().detachCopy(getJdoTemplate().getObjectById(JewelryCost.class, selectedRecord.getId()));
        if (jewelryCost.getJewelrys().isEmpty()) {
            return false;
        }
        return true;
    }
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc="checkUsingGoldsmith">
    public boolean checkUsingGoldsmith(Goldsmith selectedRecord) {
        getPersistenceManager().getFetchPlan().addGroup("detach_Goldsmith_ProductJewelry");
        getPersistenceManager().getFetchPlan().setMaxFetchDepth(-1);
        Goldsmith goldsmith = getJdoTemplate().detachCopy(getJdoTemplate().getObjectById(Goldsmith.class, selectedRecord.getId()));
        if (goldsmith.getProductJewelrys().isEmpty()) {
            return false;
        }
        return true;
    }
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc="checkUsingPricingPerson">
    public boolean checkUsingPricingPerson(PricingPerson selectedRecord) {
        getPersistenceManager().getFetchPlan().addGroup("detach_PricingPerson_ProductJewelry");
        getPersistenceManager().getFetchPlan().setMaxFetchDepth(-1);
        PricingPerson pricingPerson = getJdoTemplate().detachCopy(getJdoTemplate().getObjectById(PricingPerson.class, selectedRecord.getId()));
        if (pricingPerson.getJewelryList().isEmpty()) {
            return false;
        }
        return true;
    }
    //</editor-fold>
    
    //<editor-fold defaultstate="collapsed" desc="checkUsingDiamondPure">
    public boolean checkUsingDiamondPure(DiamondPure selectedRecord) {
        getPersistenceManager().getFetchPlan().addGroup("detach_PricingPerson_ProductJewelry");
        getPersistenceManager().getFetchPlan().setMaxFetchDepth(-1);
        DiamondPure diamondPure = getJdoTemplate().detachCopy(getJdoTemplate().getObjectById(DiamondPure.class, selectedRecord.getId()));
        if (diamondPure.getProductDiamonds().isEmpty()) {
            return false;
        }
        return true;
    }
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc="getProductGoldById">
    public ProductGold getProductGoldById(Long id) {
        getPersistenceManager().getFetchPlan().addGroup("detach_ProductGold_Unit");
        getPersistenceManager().getFetchPlan().setMaxFetchDepth(-1);
        return getJdoTemplate().detachCopy(getJdoTemplate().getObjectById(ProductGold.class, id));
    }
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc="getImportOrderByEmployeeId">
    public ImportJewerlyOrder getImportOrderByEmployeeId(Long empId, Date date,int type) {
        getPersistenceManager().getFetchPlan().addGroup("detach_ImportJewerlyOrder_All");
        getPersistenceManager().getFetchPlan().setMaxFetchDepth(-1);
        Collection<ImportJewerlyOrder> orders = getJdoTemplate().find(ImportJewerlyOrder.class, "createdBy==pEmp && createdDate >= pDate && closed==false && type == pType", "com.kmcsoft.emaster.entity.Employee pEmp, java.util.Date pDate, Integer pType", getEmployeeById(empId), DateUtil.beginOfDate(date),type);
        if (!orders.isEmpty()) {
            return getJdoTemplate().detachCopy(orders.iterator().next());
        }
        ImportJewerlyOrder importOrder = new ImportJewerlyOrder();
        importOrder.setType(type);
        importOrder.setCode(createOrderCode());
        return importOrder;

    }
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc="getEmployeeById">
    public Employee getEmployeeById(Long id) {
        return getJdoTemplate().detachCopy(getJdoTemplate().getObjectById(Employee.class, id));
    }
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc="createOrderCode">
    public int createOrderCode() {
        Collection<ImportJewerlyOrder> orders = getJdoTemplate().find(ImportJewerlyOrder.class, "createdDate >= pDate", "java.util.Date pDate", DateUtil.beginOfDate(new Date()));
        return orders.size() + 1;
    }
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc="getBarCode">
    public String getBarCode() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyMMddHHmmssSSS");
        return sdf.format(new Date());
    }
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc="getImportJewerlyOrderByProductId">
    public ImportJewerlyOrder getImportJewerlyOrderByProductId(long productId) {
        getPersistenceManager().getFetchPlan().addGroup("detach_Product_ImportJewelryDetail");
        getPersistenceManager().getFetchPlan().addGroup("detach_ImportJewelryDetail_Order");
        getPersistenceManager().getFetchPlan().addGroup("detach_ImportJewelryDetail_Order");
        getPersistenceManager().getFetchPlan().addGroup("detach_ImportJewerlyOrder_Shop");
        getPersistenceManager().getFetchPlan().addGroup("detach_ImportJewerlyOrder_Booth");
        getPersistenceManager().getFetchPlan().setMaxFetchDepth(-1);
        Product product = getJdoTemplate().detachCopy(getJdoTemplate().getObjectById(Product.class, productId));
        return product.getImportJewelryDetail().getOrder();
    }
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc="getImportJewerlyOrderById">
    public ImportJewerlyOrder getImportJewerlyOrderById(long id) {
        getPersistenceManager().getFetchPlan().addGroup("detach_ImportJewerlyOrder_All");
        getPersistenceManager().getFetchPlan().setMaxFetchDepth(-1);
        return getJdoTemplate().detachCopy(getJdoTemplate().getObjectById(ImportJewerlyOrder.class, id));
    }
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc="getImportJewerlyOrderById_DetachDetail">
    public ImportJewerlyOrder getImportJewerlyOrderById_DetachDetail(long id) {
        getPersistenceManager().getFetchPlan().addGroup("detach_ImportJewerlyOrder_ImportJewelryDetail");
        getPersistenceManager().getFetchPlan().addGroup("detach_ImportJewelryDetail_Product");
        getPersistenceManager().getFetchPlan().setMaxFetchDepth(-1);
        return getJdoTemplate().detachCopy(getJdoTemplate().getObjectById(ImportJewerlyOrder.class, id));
    }
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc="getImportJewerlyDetailById">
    public ImportJewelryDetail getImportJewerlyDetailById(long id) {
        return getJdoTemplate().detachCopy(getJdoTemplate().getObjectById(ImportJewelryDetail.class, id));
    }
    //</editor-fold>
    
    //<editor-fold defaultstate="collapsed" desc="getBasicCurrency">
    public Currency getBasicCurrency() {
        if (getBusinessInfo() != null) {
            return getCurrencyByCode(getBusinessInfo().getBankRate().getCurrency());
        }
        return null;
    }
    //</editor-fold>

    

    

    
}
